package 华为OD机试B卷复用题.进阶题;

import utils.MyUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Scanner;

public class HJ6_计算堆栈中的剩余数字 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("src/main/java/华为OD机试B卷复用题/进阶题/input/6.txt"));
        MyUtil.START();
        Solution(in);
        MyUtil.END();
    }

    // 用 List 存储结果，模拟加入集合的操作，暴力解
    static void Solution(Scanner in) {
        String[] sArray = in.nextLine().split(" ");
        int len = sArray.length;
        int[] nums = new int[len];
        for (int i = 0; i < len; i++) {
            nums[i] = Integer.parseInt(sArray[i]);
        }
        LinkedList<Integer> result = new LinkedList<>();
        for (int i = 0; i < len; i++) {
            int now = nums[i];
            int tmpTotal = 0;
            for (int j = result.size() - 1; j >= 0; j--) {
                tmpTotal += result.get(j);
                if (tmpTotal > now) {
                    break;
                }
                if (tmpTotal == now) {
                    int cnt = result.size() - j;
                    while (cnt-- > 0) {
                        result.removeLast();
                    }
                    now *= 2;
                }
            }
            result.add(now);
        }
        // 逆序输出 result
    }
}
